home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgame / kmessageio.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  11.3 KB  |  417 lines

  1. /*
  2.     This file is part of the KDE games library
  3.     Copyright (C) 2001 Burkhard Lehner (Burkhard.Lehner@gmx.de)
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License version 2 as published by the Free Software Foundation.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.     Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. /*
  21.      KMessageIO class and subclasses KMessageSocket and KMessageDirect
  22. */
  23.  
  24. #ifndef _KMESSAGEIO_H_
  25. #define _KMESSAGEIO_H_
  26.  
  27. #include <qcstring.h>
  28. #include <qhostaddress.h>
  29. #include <qobject.h>
  30. #include <qstring.h>
  31. #include <qptrqueue.h>
  32. #include <qfile.h>
  33. #include <kdebug.h>
  34.  
  35. class QSocket;
  36. class KProcess;
  37. //class QFile;
  38.  
  39.  
  40. /**
  41.   This abstract base class represents one end of a message connections
  42.   between two clients. Each client has one object of a subclass of
  43.   KMessageIO. Calling /e send() on one object will emit the signal
  44.   /e received() on the other object, and vice versa.
  45.  
  46.   For each type of connection (TCP/IP socket, COM port, direct connection
  47.   within the same class) a subclass of KMessageIO must be defined that
  48.   implements the pure virtual methods /e send() and /e isConnected(),
  49.   and sends the signals. (See /e KMessageSocket for an example implementation.)
  50.  
  51.   Two subclasses are already included: /e KMessageSocket (connection using
  52.   TCP/IP sockets) and /e KMessageDirect (connection using method calls, both
  53.   sides must be within the same process).
  54. */
  55.  
  56. class KMessageIO : public QObject
  57. {
  58.   Q_OBJECT
  59.  
  60. public:
  61.   /**
  62.    * The usual QObject constructor, does nothing else.
  63.    **/
  64.   KMessageIO (QObject *parent = 0, const char *name = 0);
  65.  
  66.   /**
  67.    * The usual destructor, does nothing special.
  68.    **/
  69.   ~KMessageIO ();
  70.  
  71.   /**
  72.   * The runtime idendifcation
  73.   */
  74.   virtual int rtti() const {return 0;}
  75.  
  76.   /**
  77.    * @return Whether this KMessageIO is a network IO or not.
  78.    **/
  79.   //virtual bool isNetwork () const = 0;
  80.   virtual bool isNetwork () const
  81.   {
  82.    kdError(11001) << "Calling PURE virtual isNetwork...BAD" << endl;
  83.    return false;
  84.   }
  85.  
  86.   /**
  87.     This method returns the status of the object, whether it is already
  88.     (or still) connected to another KMessageIO object or not.
  89.  
  90.     This is a pure virtual method, so it has to be implemented in a subclass
  91.     of KMessageIO.
  92.   */
  93.   //virtual bool isConnected () const = 0;
  94.   virtual bool isConnected () const
  95.   {
  96.    kdError(11001) << "Calling PURE virtual isConencted...BAD" << endl;
  97.    return false;
  98.   }
  99.  
  100.   /**
  101.     Sets the ID number of this object. This number can for example be used to
  102.     distinguish several objects in a server.
  103.  
  104.     NOTE: Sometimes it is useful to let two connected KMessageIO objects
  105.     have the same ID number. You have to do so yourself, KMessageIO doesn't
  106.     change this value on its own!
  107.   */
  108.   void setId (Q_UINT32 id);
  109.  
  110.   /**
  111.     Queries the ID of this object.
  112.   */
  113.   Q_UINT32 id ();
  114.  
  115.   /**
  116.     @since 3.2
  117.     @return 0 in the default implementation. Reimplemented in @ref KMessageSocket.
  118.   */
  119.   virtual Q_UINT16 peerPort () const { return 0; }
  120.  
  121.   /**
  122.     @since 3.2
  123.     @return "localhost" in the default implementation. Reimplemented in @ref KMessageSocket
  124.   */
  125.   virtual QString peerName () const { return QString::fromLatin1("localhost"); }
  126.  
  127.  
  128. signals:
  129.   /**
  130.     This signal is emitted when /e send() on the connected KMessageIO
  131.     object is called. The parameter contains the same data array in /e msg
  132.     as was used in /e send().
  133.   */
  134.   void received (const QByteArray &msg);
  135.  
  136.   /**
  137.     This signal is emitted when the connection is closed. This can be caused
  138.     by a hardware error (e.g. broken internet connection) or because the other
  139.     object was killed.
  140.  
  141.     Note: Sometimes a broken connection can be undetected for a long time,
  142.     or may never be detected at all. So don't rely on this signal!
  143.   */
  144.   void connectionBroken ();
  145.  
  146. public slots:
  147.  
  148.   /**
  149.     This slot sends the data block in /e msg to the connected object, that will
  150.     emit /e received().
  151.  
  152.     For a concrete class, you have to subclass /e KMessageIO and overwrite this
  153.     method. In the subclass, implement this method as an ordinary method, not
  154.     as a slot! (Otherwise another slot would be defined. It would work, but uses
  155.     more memory and time.) See /e KMessageSocket for an example implementation.
  156.   */
  157.   virtual void send (const QByteArray &msg) = 0;
  158.  
  159. protected:
  160.   Q_UINT32 m_id;
  161. };
  162.  
  163.  
  164. /**
  165.   This class implements the message communication using a TCP/IP socket. The
  166.   object can connect to a server socket, or can use an already connected socket.
  167. */
  168.  
  169. class KMessageSocket : public KMessageIO
  170. {
  171.   Q_OBJECT
  172.  
  173. public:
  174.   /**
  175.     Connects to a server socket on /e host with /e port. host can be an
  176.     numerical (e.g. "192.168.0.212") or symbolic (e.g. "wave.peter.org")
  177.     IP address. You can immediately use the /e sendSystem() and
  178.     /e sendBroadcast() methods. The messages are stored and sent to the
  179.     receiver after the connection is established.
  180.  
  181.     If the connection could not be established (e.g. unknown host or no server
  182.     socket at this port), the signal /e connectionBroken is emitted.
  183.   */
  184.   KMessageSocket (QString host, Q_UINT16 port, QObject *parent = 0,
  185.                   const char *name = 0);
  186.  
  187.   /**
  188.     Connects to a server socket on /e host with /e port. You can immediately
  189.     use the /e sendSystem() and /e sendBroadcast() methods. The messages are
  190.     stored and sent to the receiver after the connection is established.
  191.  
  192.     If the connection could not be established (e.g. unknown host or no server
  193.     socket at this port), the signal /e connectionBroken is emitted.
  194.   */
  195.   KMessageSocket (QHostAddress host, Q_UINT16 port, QObject *parent = 0,
  196.                   const char *name = 0);
  197.  
  198.   /**
  199.     Uses /e socket to do the communication.
  200.  
  201.     The socket should already be connected, or at least be in /e connecting
  202.     state.
  203.  
  204.     Note: The /e socket object is then owned by the /e KMessageSocket object.
  205.     So don't use it otherwise any more and don't delete it. It is deleted
  206.     together with this KMessageSocket object. (Use 0 as parent for the QSocket
  207.     object t ensure it is not deleted.)
  208.   */
  209.   KMessageSocket (QSocket *socket, QObject *parent = 0, const char *name = 0);
  210.  
  211.   /**
  212.     Uses the socket specified by the socket descriptor socketFD to do the
  213.     communication. The socket must already be connected.
  214.  
  215.     This constructor can be used with a QServerSocket within the (pure
  216.     virtual) method /e newConnection.
  217.  
  218.     Note: The socket is then owned by the /e KMessageSocket object. So don't
  219.     manipulate the socket afterwards, especially don't close it. The socket is
  220.     automatically closed when KMessageSocket is deleted.
  221.   */
  222.   KMessageSocket (int socketFD, QObject *parent = 0, const char *name = 0);
  223.  
  224.   /**
  225.     Destructor, closes the socket.
  226.   */
  227.   ~KMessageSocket ();
  228.  
  229.   /**
  230.   * The runtime idendifcation
  231.   */
  232.   virtual int rtti() const {return 1;}
  233.  
  234.   /**
  235.     @since 3.2
  236.     @return The port that this object is connected to. See QSocket::peerPort
  237.   */
  238.   virtual Q_UINT16 peerPort () const;
  239.  
  240.   /**
  241.     @since 3.2
  242.     @return The hostname this object is connected to. See QSocket::peerName.
  243.   */
  244.   virtual QString peerName () const;
  245.  
  246.   /**
  247.     @return TRUE as this is a network IO.
  248.   */
  249.   bool isNetwork() const { return true; }
  250.  
  251.   /**
  252.     Returns true if the socket is in state /e connected.
  253.   */
  254.   bool isConnected () const;
  255.  
  256.   /**
  257.     Overwritten slot method from KMessageIO.
  258.  
  259.     Note: It is not declared as a slot method, since the slot is already
  260.     defined in KMessageIO as a virtual method.
  261.   */
  262.   void send (const QByteArray &msg);
  263.  
  264. protected slots:
  265.   virtual void processNewData ();
  266.  
  267. protected:
  268.   void initSocket ();
  269.   QSocket *mSocket;
  270.   bool mAwaitingHeader;
  271.   Q_UINT32 mNextBlockLength;
  272.  
  273.   bool isRecursive;  // workaround for "bug" in QSocket, Qt 2.2.3 or older
  274. };
  275.  
  276.  
  277. /**
  278.   This class implements the message communication using function calls
  279.   directly. It can only be used when both sides of the message pipe are
  280.   within the same process. The communication is very fast.
  281.  
  282.   To establish a communication, you have to create two instances of
  283.   KMessageDirect, the first one with no parameters in the constructor,
  284.   the second one with the first as parameter:
  285.  
  286.   /code
  287.     KMessageDirect *peer1, *peer2;
  288.     peer1 = new KMessageDirect ();       // unconnected
  289.     peer2 = new KMessageDirect (peer1);  // connect with peer1
  290.   /endcode
  291.  
  292.   The connection is only closed when one of the instances is deleted.
  293. */
  294.  
  295. class KMessageDirect : public KMessageIO
  296. {
  297.   Q_OBJECT
  298.  
  299. public:
  300.   /**
  301.     Creates an object and connects it to the object given in the first
  302.     parameter. Use 0 as first parameter to create an unconnected object,
  303.     that is later connected.
  304.  
  305.     If that object is already connected, the object remains unconnected.
  306.   */
  307.   KMessageDirect (KMessageDirect *partner = 0, QObject *parent = 0, const char
  308. *name = 0);
  309.  
  310.   /**
  311.     Destructor, closes the connection.
  312.   */
  313.   ~KMessageDirect ();
  314.  
  315.   /**
  316.   * The runtime idendifcation
  317.   */
  318.   virtual int rtti() const {return 2;}
  319.  
  320.  
  321.   /**
  322.     @return FALSE as this is no network IO.
  323.   */
  324.   bool isNetwork() const { return false; }
  325.  
  326.   /**
  327.     Returns true, if the object is connected to another instance.
  328.  
  329.     If you use the first constructor, the object is unconnected unless another
  330.     object is created with this one as parameter.
  331.  
  332.     The connection can only be closed by deleting one of the objects.
  333.   */
  334.   bool isConnected () const;
  335.  
  336.   /**
  337.     Overwritten slot method from KMessageIO.
  338.  
  339.     Note: It is not declared as a slot method, since the slot is already
  340.     defined in KMessageIO as a virtual method.
  341.   */
  342.   void send (const QByteArray &msg);
  343.  
  344. protected:
  345.   KMessageDirect *mPartner;
  346. };
  347.  
  348. class KMessageProcess : public KMessageIO
  349. {
  350.   Q_OBJECT 
  351.  
  352.   public:
  353.     KMessageProcess(QObject *parent, QString file);
  354.     ~KMessageProcess();
  355.     bool isConnected() const;
  356.     void send (const QByteArray &msg);
  357.     void writeToProcess();
  358.  
  359.     /**
  360.       @return FALSE as this is no network IO.
  361.     */
  362.     bool isNetwork() const { return false; }
  363.  
  364.   /**
  365.   * The runtime idendifcation
  366.   */
  367.   virtual int rtti() const {return 3;}
  368.  
  369.  
  370.  
  371.   public slots:
  372.   void  slotReceivedStdout(KProcess *proc, char *buffer, int buflen);
  373.   void  slotReceivedStderr(KProcess *proc, char *buffer, int buflen);
  374.   void  slotProcessExited(KProcess *p);
  375.   void  slotWroteStdin(KProcess *p);
  376.  
  377.   private:
  378.     QString mProcessName;
  379.     KProcess *mProcess;
  380.     QPtrQueue <QByteArray> mQueue;
  381.     QByteArray *mSendBuffer;
  382.     QByteArray mReceiveBuffer;
  383.     unsigned int mReceiveCount;
  384. };
  385.  
  386. class KMessageFilePipe : public KMessageIO
  387. {
  388.   Q_OBJECT 
  389.  
  390.   public:
  391.     KMessageFilePipe(QObject *parent,QFile *readFile,QFile *writeFile);
  392.     ~KMessageFilePipe();
  393.     bool isConnected() const;
  394.     void send (const QByteArray &msg);
  395.     void exec();
  396.  
  397.     /**
  398.       @return FALSE as this is no network IO.
  399.     */
  400.     bool isNetwork() const { return false; }
  401.  
  402.   /**
  403.   * The runtime idendifcation
  404.   */
  405.   virtual int rtti() const {return 4;}
  406.  
  407.  
  408.  
  409.   private:
  410.     QFile *mReadFile;
  411.     QFile *mWriteFile;
  412.     QByteArray mReceiveBuffer;
  413.     unsigned int mReceiveCount;
  414. };
  415.  
  416. #endif
  417.